package eproject.mm.com.projectsupervision.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.dinuscxj.progressbar.CircleProgressBar;

import java.util.List;
import java.util.Map;

import eproject.mm.com.projectsupervision.R;

/**
 * Created by momotea on 2018/3/15.
 */

public class ProjectAdapter extends BaseAdapter {
    private List<Map> list;
    private Context context;
    private LayoutInflater inflater;

    public ProjectAdapter(Context context,List list){
        this.list = list;
        inflater = LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        return list.size();
    }
    @Override
    public Map getItem(int position) {
        return list.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = new ViewHolder();
        final Map item = getItem(position);

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.project_item, null);
            holder.tv_prjName = (TextView) convertView.findViewById(R.id.tv_prjName);
            holder.tv_curSchedule = (TextView) convertView.findViewById(R.id.tv_curSchedule);
            holder.tv_buildUnit = (TextView) convertView.findViewById(R.id.tv_buildUnit);
            holder.pBar = (CircleProgressBar) convertView.findViewById(R.id.pb_rate);

            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv_prjName.setText((String)item.get("name"));
        String percentdoneStr = (String)item.get("percentdone");
        if(percentdoneStr==null)percentdoneStr="0";
        holder.tv_curSchedule.setText(percentdoneStr+"%");
        List buList = (List)item.get("buildunits");
        String bus = "";
        if(buList!=null){
            for(int i=0;i<buList.size();i++){
                if(!bus.equals("")){
                    bus += "、";
                }
                Map unitMap = (Map)buList.get(i);
                bus += (String)unitMap.get("buildname");
            }
        }
        holder.tv_buildUnit.setText(bus);
        try {
            String percentdone=(String)item.get("percentdone");
            if(percentdone.indexOf(".")>0){
                percentdone = percentdone.substring(0,percentdone.indexOf("."));
            }
            holder.pBar.setProgress(Integer.parseInt(percentdone));
        }catch (Exception e){
            holder.pBar.setProgress(0);
        }
        return convertView;
    }

    private static class ViewHolder {
        TextView tv_prjName;
        TextView tv_curSchedule;
        TextView tv_buildUnit;
        CircleProgressBar pBar;
    }
}
